package com.diyphonegadgets.freeblog.utils;

import android.util.Log;

public class Debug {

	private static final String TAG = "DEBUG";

	private static final int DEBUG_TEMP = 0x1;
	private static final int DEBUG_PERM = 0x10;
	private static final int DEBUG_INFO = 0x100;
	private static final int DEBUG_TRACE = 0x1000;
	private static final int DEBUG_WARNING = 0x10000;

	private static final int DEBUG_LEVEL = DEBUG_TEMP | DEBUG_PERM | DEBUG_INFO
			| DEBUG_TRACE | DEBUG_WARNING;

	private static final int CUT_STRING_LENGTH = 200;
	private static long mnTime = 0;

	private static boolean debugTempAvailable() {
		// return (DEBUG_LEVEL&DEBUG_TEMP) > 0;

		return true;
	}

	private static boolean debugPermAvailable() {
		return (DEBUG_LEVEL & DEBUG_PERM) > 0;
	}

	private static boolean debugInfoAvailable() {
		return (DEBUG_LEVEL & DEBUG_INFO) > 0;
	}

	private static boolean debugTraceAvailable() {
		return (DEBUG_LEVEL & DEBUG_TRACE) > 0;
	}

	private static boolean debugWarningAvailable() {
		return (DEBUG_LEVEL & DEBUG_WARNING) > 0;
	}

	public static void debugTemporaryLargeString(String tag, String text) {
		if (debugTempAvailable()) {
			int loop = 0;
			while (text.length() > CUT_STRING_LENGTH) {
				Log.i(tag, text.substring(CUT_STRING_LENGTH * loop,
						CUT_STRING_LENGTH * (loop + 1)));
				text = text.substring(CUT_STRING_LENGTH * (loop + 1));
			}
			Log.i(tag, text);
		}
	}

	public static void debugTemporary(String tag, String text) {
		if (debugTempAvailable()) {
			Log.i(tag, text);
		}
	}

	public static void debugPermanent(String tag, String text) {
		if (debugPermAvailable()) {
			Log.i(tag, text);
		}
	}

	public static void debugInfo(String tag, String text) {
		if (debugInfoAvailable()) {
			Log.i(tag, text);
		}
	}

	public static void debugTrace(String tag, String text) {
		if (debugTraceAvailable()) {
			Log.i(tag, text);
		}
	}

	public static void debugWarning(String tag, String text) {
		if (debugWarningAvailable()) {
			Log.w(tag, text);
		}
	}

	public static void startTimeMeasure() {
		mnTime = System.currentTimeMillis();
	}

	public static void getCurrentTimeMeasure(boolean stopMeasure) {
		if (mnTime > 0) {
			debugPermanent(TAG,
					"-- Current time measure : "
							+ (System.currentTimeMillis() - mnTime));
			if (stopMeasure) {
				mnTime = 0;
			}
		} else {
			debugWarning(TAG, "-- startTimeMeasure hasn't been called before");
		}
	}

	public static void getCurrentTimeMeasureAndReset() {
		if (mnTime > 0) {
			debugPermanent(TAG,
					"-- Current time measure : "
							+ (System.currentTimeMillis() - mnTime));
			startTimeMeasure();
		} else {
			debugWarning(TAG, "-- startTimeMeasure hasn't been called before");
		}
	}
}
